//###############
// FILE : FilterReader.java
// WRITER : Lior Zimmerman liorz06 066462300
// WRITER : Ohad Dan ohad_dan 036767960
// EXERCISE : oop ex3 2008
// DESCRIPTION: represents a filter reader
//###############
package oop.ex3.fileSearchModel.util;
import java.io.*;

import oop.ex3.fileSearchModel.util.exceptions.ReaderIOException;
/**
 * this class extends the ability of bufferedReader, it returns an iterator
 * to go through the file system. 
 * @author liorz06
 * @author ohad_dan
 */
public class FilterReader {
	//*******************************************************************
	//  Data members :                                                  *
	//*******************************************************************
	private String _source;
    private BufferedReader _reader; 
    //********************************************************************
	//  Finals :                                                         *
	//********************************************************************
	private final String IO_EXCEPTION_MESSAGE = "Error:I/O failure";
    //*******************************************************************
	//  Methods :                                                       *
	//*******************************************************************
   /**
    * Constructs a new filter reader.
    * @param source File's Source directory.
    * @throws ReaderIOException In case of I/O failure.
    */
    public FilterReader(String source) throws ReaderIOException{
        _source = source;
        try{
            _reader = new BufferedReader( new FileReader( _source ) );
        }
        catch( IOException e) {
            throw new ReaderIOException(IO_EXCEPTION_MESSAGE);
        }
    }
    /**
     * Returns an iterator over the file collection.
     * @return An iterator over the file collection.
     * @throws ReaderIOException In case of I/O failure.
     */
    public FileIterator iterator() throws ReaderIOException{
        return new FileReaderIterator(_reader);
    }
    /**
     * Close the reader.
     * @throws ReaderIOException In case of I/O failure.
     */
    public void close() throws ReaderIOException{
        try{
            _reader.close();
        }
        catch( IOException e ) {
            throw new ReaderIOException(IO_EXCEPTION_MESSAGE);
        }
    }
}
